package com.ruoyi.server.read;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.resource.mapper.FriendMapper;
import com.ruoyi.resource.mapper.GroupMapper;
import com.ruoyi.resource.service.FriendService;
import com.ruoyi.server.enums.AddConstants5;
import com.ruoyi.server.enums.CmdConstants;
import com.ruoyi.server.enums.PullConstants3;
import com.ruoyi.server.mapper.SendMsgMapper;
import com.ruoyi.server.person.PassageWayUser;
import com.ruoyi.server.pkg.SendMsg;
import com.ruoyi.server.thread.ReadReceiptReader;
import com.ruoyi.server.utils.AddUtil;
import com.ruoyi.server.utils.ApplicationUtil;
import com.ruoyi.server.utils.OnlineUtil;
import com.ruoyi.system.service.ISysUserService;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.*;

/**
 *  群聊拉取
 *
 * @author OpenACloud
 * @create 2024/6/3 21:38
 */
@Component
public class GroupReader extends BasePictureReader {

    @Resource
    protected SendMsgMapper sendMsgMapper;
    @Resource
    protected FriendService friendService;
    @Resource
    protected GroupMapper groupMapper;
    @Resource
    protected RedisCache redisCache;
    @Resource
    protected ISysUserService userService;

    @Override
    protected void sendFriendOrGroupMsg(ChannelHandlerContext ctx, SendMsg sendMsg) {
        if (Objects.isNull(sendMsg.getSetGroup())) {
            
        }
        for (Long userId : sendMsg.getSetGroup()) {
            ChannelHandlerContext userCtx = OnlineUtil.ONLINE_MAPS.get(userId);
            userCtx.channel().writeAndFlush(sendMsg);
        }
        this.saveMessages(sendMsg);
    }

    @Override
    protected void deleteFriendOrGroup(ChannelHandlerContext ctx, SendMsg sendMsg) {

    }

    @Override
    protected void addFriendOrGroup(ChannelHandlerContext ctx, SendMsg sendMsg) {
        Set<Long> meGroup = (Set<Long>) groupMapper.findMeGroup(null);
        Set<Long> apSaveUser = AddUtil.addGroup(ctx, sendMsg, AddUtil.produceReplyMsg(AddConstants5.ADD_FRIEND, sendMsg.SENDID), meGroup);
        HashMap<Long, Set<Long>> apUser = ctx.attr(AddUtil.ADD_APPLICATION_GROUP).get();
        apUser.put(sendMsg.RECEIVEID, apSaveUser);
        ctx.attr(AddUtil.ADD_APPLICATION_GROUP).set(apUser);
    }

    @Override
    protected void apFriendOrGroupProcessing(ChannelHandlerContext ctx, SendMsg sendMsg) {
        if(ApplicationUtil.applicationGroup(ctx, sendMsg)) {

        }
    }


    @Override
    public void saveMessages(SendMsg sendMsg) {
        sendMsgMapper.saveGroupMessages(sendMsg);
    }

    @Override
    public void pullMessages(ChannelHandlerContext ctx, PassageWayUser wayUser) {
        SendMsg sendMsg = new SendMsg();
        sendMsg.CMD = PullConstants3.PULL_MESSAGES;
        sendMsg.STATUS = PullConstants3.PULL_END_STATUS;
        sendMsg.SENDID = -1L;
        sendMsg.CONTEXT = "END PULL MESSAGES";
        sendMsg.TIME = LocalDateTime.now();
        ctx.writeAndFlush(new TextWebSocketFrame(JSONObject.toJSONString(sendMsg)));
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {

    }

}
